/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ABMEmpresa.java
 *
 * Created on 20/11/2011, 11:34:00 PM
 */
package Interface;

import Dominio.Controladora;
import Dominio.Empresa;
import java.awt.Event;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Agustin
 */
public class ABMEmpresa extends javax.swing.JFrame {

    private static ABMEmpresa instance;

    public static ABMEmpresa getInstance() {
        if (instance == null) {
            instance = new ABMEmpresa();
        }
        instance.limpiar();
        return instance;
    }

    /** Creates new form ABMEmpresa */
    public ABMEmpresa() {
        initComponents();
        this.setLocationRelativeTo(null);
        limpiar();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblAMBEmpresa = new javax.swing.JLabel();
        btnAgregarEmpresa = new javax.swing.JButton();
        txtRut = new javax.swing.JTextField();
        lblRutEmpresa = new javax.swing.JLabel();
        lblRazonSocial = new javax.swing.JLabel();
        txtRazonSocial = new javax.swing.JTextField();
        btnEliminarEmpresa = new javax.swing.JButton();
        lblNombreEmpresa = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        btnModificarEmpresa = new javax.swing.JButton();
        lbltelefono = new javax.swing.JLabel();
        lblBarrio = new javax.swing.JLabel();
        lblDireccion = new javax.swing.JLabel();
        txtDireccion = new javax.swing.JTextField();
        txtBarrio = new javax.swing.JTextField();
        txtTelefono = new javax.swing.JTextField();
        btnPersonal = new javax.swing.JButton();
        btnLimpiar = new javax.swing.JButton();
        lblRutEmpresa1 = new javax.swing.JLabel();
        lblRutEmpresa2 = new javax.swing.JLabel();
        lblRutEmpresa3 = new javax.swing.JLabel();
        lblRutEmpresa4 = new javax.swing.JLabel();
        lblRutEmpresa5 = new javax.swing.JLabel();
        lblRutEmpresa6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        lblAMBEmpresa.setFont(new java.awt.Font("Monospaced", 0, 24));
        lblAMBEmpresa.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblAMBEmpresa.setText("ABM Empresa");

        btnAgregarEmpresa.setText("+");
        btnAgregarEmpresa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarEmpresaActionPerformed(evt);
            }
        });

        lblRutEmpresa.setText("Rut Empresa");

        lblRazonSocial.setText("Razon Social");

        txtRazonSocial.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtRazonSocialFocusLost(evt);
            }
        });

        btnEliminarEmpresa.setText("-");
        btnEliminarEmpresa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarEmpresaActionPerformed(evt);
            }
        });

        lblNombreEmpresa.setText("Nombre Empresa");

        btnModificarEmpresa.setText("*");
        btnModificarEmpresa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarEmpresaActionPerformed(evt);
            }
        });

        lbltelefono.setText("Telefono");

        lblBarrio.setText("Barrio");

        lblDireccion.setText("Direccion");

        btnPersonal.setText("Personal");
        btnPersonal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPersonalActionPerformed(evt);
            }
        });

        btnLimpiar.setText("Limpiar");
        btnLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiarActionPerformed(evt);
            }
        });

        lblRutEmpresa1.setText("*");

        lblRutEmpresa2.setText("*");

        lblRutEmpresa3.setText("*");

        lblRutEmpresa4.setText("*");

        lblRutEmpresa5.setText("*");

        lblRutEmpresa6.setText("*");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblAMBEmpresa, javax.swing.GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblDireccion)
                            .addComponent(lblNombreEmpresa)
                            .addComponent(lblRazonSocial)
                            .addComponent(lblRutEmpresa)
                            .addComponent(lblBarrio)
                            .addComponent(lbltelefono))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtRazonSocial, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtRut, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(2, 2, 2)
                                        .addComponent(lblRutEmpresa1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(lblRutEmpresa3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(lblRutEmpresa2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(lblRutEmpresa4, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(btnAgregarEmpresa, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btnEliminarEmpresa, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btnModificarEmpresa, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btnPersonal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtBarrio, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lblRutEmpresa5, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lblRutEmpresa6, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE))
                    .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblAMBEmpresa)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnAgregarEmpresa)
                        .addGap(18, 18, 18)
                        .addComponent(btnEliminarEmpresa)
                        .addGap(18, 18, 18)
                        .addComponent(btnModificarEmpresa)
                        .addGap(18, 18, 18)
                        .addComponent(btnPersonal))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblRutEmpresa)
                            .addComponent(txtRut, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblRutEmpresa1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblRazonSocial)
                            .addComponent(txtRazonSocial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblRutEmpresa2))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblNombreEmpresa)
                            .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblRutEmpresa3))
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblDireccion)
                            .addComponent(txtDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblRutEmpresa4))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblBarrio)
                            .addComponent(txtBarrio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblRutEmpresa5))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lbltelefono)
                            .addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblRutEmpresa6))))
                .addGap(18, 18, 18)
                .addComponent(btnLimpiar)
                .addContainerGap(16, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void limpiar() {
        txtRut.setEnabled(true);
        txtRazonSocial.setEnabled(true);
        txtBarrio.setText("");
        txtDireccion.setText("");
        txtNombre.setText("");
        txtRazonSocial.setText("");
        txtRut.setText("");
        txtTelefono.setText("");
        btnAgregarEmpresa.setEnabled(false);
        btnEliminarEmpresa.setEnabled(false);
        btnModificarEmpresa.setEnabled(false);
        btnPersonal.setEnabled(false);
    }

    private void txtRazonSocialFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtRazonSocialFocusLost
        try {
            Empresa e = new Empresa();
            e = Controladora.getInstance().traerEmpresasActivas(txtRut.getText()).get(0);
            if (e.getRazon_social() != null) {
                if (e.getRazon_social().equals(txtRazonSocial.getText())) {
                    btnAgregarEmpresa.setEnabled(false);
                    txtRut.setEnabled(false);
                    txtRazonSocial.setEnabled(false);
                    txtBarrio.setText(e.getBarrio());
                    txtDireccion.setText(e.getDireccion());
                    txtNombre.setText(e.getNombre());
                    txtTelefono.setText(e.getTelefono());
                    btnEliminarEmpresa.setEnabled(true);
                    btnModificarEmpresa.setEnabled(true);
                    btnPersonal.setEnabled(true);
                    Controladora.getInstance().setEmpresa(e);
                } else {
                    btnAgregarEmpresa.setEnabled(false);
                    throw new Exception("El Rut no Coincide con la Razon Social");
                }
            } else {
                e = Controladora.getInstance().traerEmpresasInactivas(txtRut.getText().trim()).get(0);
                if (e.getRazon_social() != null) {
                    if (e.getRazon_social().equals(txtRazonSocial.getText().trim())) {
                        int opcion = JOptionPane.showConfirmDialog(this, "La Empresa se Encuentra Dada de Baja, desea Reabilitarla ", "Atencion", JOptionPane.YES_NO_OPTION);
                        if (opcion == JOptionPane.YES_OPTION) {
                            int x = Controladora.getInstance().crearEmpresa(txtRut.getText(), txtRazonSocial.getText(), "*", "*", "*", "*");
                            if (x == 1) {
                                btnAgregarEmpresa.setEnabled(false);
                                throw new Exception("El Rut no Coincide con la Razon Social");

                            } else if (x == 2) {
                                limpiar();
                                JOptionPane.showMessageDialog(this, "La Empresa Ha Sido Reactivada", "Listo", JOptionPane.INFORMATION_MESSAGE);
                            }
                        }
                    } else {
                        throw new Exception("El Rut no Coincide con la Razon Social");
                    }
                } else {
                    if (Controladora.getInstance().controlarRazonSocial(txtRazonSocial.getText().trim(), txtRut.getText().trim())) {
                        btnAgregarEmpresa.setEnabled(true);
                    } else {
                        btnAgregarEmpresa.setEnabled(false);
                        throw new Exception("El Rut no Coincide con la Razon Social");
                    }

                }
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, Controladora.getInstance().limpiarE(e), "Atencion", JOptionPane.ERROR_MESSAGE);
        }



    }//GEN-LAST:event_txtRazonSocialFocusLost

    private void btnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiarActionPerformed
        limpiar();
    }//GEN-LAST:event_btnLimpiarActionPerformed

    private void btnEliminarEmpresaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarEmpresaActionPerformed
        try {
            Controladora.getInstance().bajaEmpresa(txtRut.getText());
            limpiar();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, Controladora.getInstance().limpiarE(e), "Atencion", JOptionPane.ERROR_MESSAGE);
        }



    }//GEN-LAST:event_btnEliminarEmpresaActionPerformed
    private void btnAgregarEmpresaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarEmpresaActionPerformed
        try {
            Controladora.getInstance().crearEmpresa(txtRut.getText(), txtRazonSocial.getText(), txtNombre.getText(), txtDireccion.getText(), txtBarrio.getText(), txtTelefono.getText());
            JOptionPane.showMessageDialog(this, "La Empresa Ha Sido Creada Correctamente ", "Listo", JOptionPane.INFORMATION_MESSAGE);
            txtBarrio.setCursor(null);
            limpiar();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, Controladora.getInstance().limpiarE(e), "Atencion", JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_btnAgregarEmpresaActionPerformed

    private void btnModificarEmpresaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarEmpresaActionPerformed
        try {
            Controladora.getInstance().modificarEmpresa(txtRut.getText(), txtRazonSocial.getText(), txtNombre.getText(), txtDireccion.getText(), txtBarrio.getText(), txtTelefono.getText());
            limpiar();
            JOptionPane.showMessageDialog(this, "La Empresa Ha Sido Modificada Correctamente ", "Listo", JOptionPane.INFORMATION_MESSAGE);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, Controladora.getInstance().limpiarE(e), "Atencion", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnModificarEmpresaActionPerformed

    private void btnPersonalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPersonalActionPerformed
        Personal.getInstance().setVisible(true);
    }//GEN-LAST:event_btnPersonalActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ABMEmpresa().setVisible(true);






            }
        });






    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregarEmpresa;
    private javax.swing.JButton btnEliminarEmpresa;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JButton btnModificarEmpresa;
    private javax.swing.JButton btnPersonal;
    private javax.swing.JLabel lblAMBEmpresa;
    private javax.swing.JLabel lblBarrio;
    private javax.swing.JLabel lblDireccion;
    private javax.swing.JLabel lblNombreEmpresa;
    private javax.swing.JLabel lblRazonSocial;
    private javax.swing.JLabel lblRutEmpresa;
    private javax.swing.JLabel lblRutEmpresa1;
    private javax.swing.JLabel lblRutEmpresa2;
    private javax.swing.JLabel lblRutEmpresa3;
    private javax.swing.JLabel lblRutEmpresa4;
    private javax.swing.JLabel lblRutEmpresa5;
    private javax.swing.JLabel lblRutEmpresa6;
    private javax.swing.JLabel lbltelefono;
    private javax.swing.JTextField txtBarrio;
    private javax.swing.JTextField txtDireccion;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtRazonSocial;
    private javax.swing.JTextField txtRut;
    private javax.swing.JTextField txtTelefono;
    // End of variables declaration//GEN-END:variables
}
